package com.controller;

import com.entity.Favour;
import com.google.gson.Gson;
import com.service.impl.BlogServiceImpl;
import com.service.impl.FavourServiceImpl;
import com.service.intf.BlogService;
import com.service.intf.FavourService;
import com.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FavourServlet  extends BaseServlet{
        FavourService favourService =new FavourServiceImpl();
    BlogService blogService =new BlogServiceImpl();

    protected void isFavour(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        int userId = WebUtils.parseInt(req.getParameter("userId"), 0);
//        Integer userId = (Integer) req.getSession().getAttribute("userId");
        String username = req.getParameter("username");
//        System.out.println(username+"???");
        Integer userId= blogService.queryUserIdByUserName(username);
        int blogId = WebUtils.parseInt(req.getParameter("blogId"), 0);
        boolean favour = favourService.isFavour(blogId, userId);
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("favour",favour);

        Gson gson=new Gson();
        String json = gson.toJson(resultMap);

        resp.getWriter().write(json);

//        req.getRequestDispatcher("/pages/blog/blog_list.jsp").forward(req, resp);

    }


    protected void saveFavour(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        Integer userId= blogService.queryUserIdByUserName(username);

//        Integer userId = (Integer) req.getSession().getAttribute("userId");
        int blogId = WebUtils.parseInt(req.getParameter("blogId"), 0);
        favourService.saveFavour(blogId,userId);

        resp.sendRedirect(req.getContextPath()+"/user/favourServlet?action=isFavour&blogId="+blogId);

    }

    /**
     * 查询某篇博客的获赞数
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteFavour(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        Integer userId= blogService.queryUserIdByUserName(username);
//        Integer userId = (Integer) req.getSession().getAttribute("userId");
        int blogId = WebUtils.parseInt(req.getParameter("blogId"), 0);
        favourService.deleteFavour(blogId,userId);

        resp.sendRedirect(req.getContextPath()+"/user/favourServlet?action=isFavour&blogId="+blogId);

    }

    protected void queryFavourByBlogId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int blogId = WebUtils.parseInt(req.getParameter("blogId"), 0);
        //queryFavourByBlogId是查询用户id的
        List<Favour> favours = favourService.queryFavourByBlogId(blogId);
//        System.out.println(favours.size());
        req.getSession().setAttribute("favourNum",favours.size());


//        然后跳转到CollectionServlet去查询具体的收藏数！
        resp.sendRedirect(req.getContextPath()+"/user/collectionServlet?action=queryCollectionNumByBlogId&blogId="+blogId);
//        req.getRequestDispatcher("/pages/blog/blog_detail.jsp").forward(req, resp);
    }

    protected void queryUserFavour(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        Integer userId = blogService.queryUserIdByUserName(username);
        Integer queryUserFavour = favourService.queryUserFavour(userId);
        req.setAttribute("queryUserFavour",queryUserFavour);
        req.getRequestDispatcher("/pages/follow/queryUserFavour.jsp").forward(req,resp);

    }


}
